package com.example.controller.auth;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.auth.domain.User;
import com.example.config.auth.SecurityUtil;
import com.example.domain.vo.UserVo;
import com.example.feign.UserFeignService;
import com.example.util.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@Slf4j
@Api(tags = "用户服务")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("delete/userId")
    public JSONObject deleteUserId(@RequestParam("userId") String userId) {
        return userFeignService.deleteUser(userId);
    }

    @PostMapping("updatepwd")
    public JSONObject updatePwd(@RequestBody @Validated UserVo vo) {
        return userFeignService.updatePassword(JSONObject.parseObject(JSONObject.toJSONString(vo)));
    }

    @GetMapping("/getcount")
    public JSONObject getUserCount() {
        Integer count=userFeignService.findUserTotalHash();
        return Results.success(count);
    }

    @GetMapping("/page")
    public JSONObject pageList(@RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                               @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                               @RequestParam(name = "userName",required = false)String userName){
       return userFeignService.getPageList(pageNum,pageSize,userName);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register/hash")
    public ResponseEntity<?> registerUserHash(@RequestBody @Validated UserVo vo){
        JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(vo));
        JSONObject jsonObject=userFeignService.registerUserHash(json);
        log.info(jsonObject.toJSONString());
        return ResponseEntity.ok(jsonObject);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseEntity<?> registerUser(@RequestBody @Validated UserVo vo){
        JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(vo));
        JSONObject jsonObject=userFeignService.registerUser(json);
        log.info(jsonObject.toJSONString());
        return ResponseEntity.ok(jsonObject);
    }

    @ApiOperation("获取用户通过用户名")
    @PostMapping("/getuser/username")
    public ResponseEntity<?> getUserByUserName(@RequestParam("userName")String userName){
        User user =userFeignService.getUserByHashUserName(userName);
        log.info(JSONObject.toJSONString(user));
        return ResponseEntity.ok(user);
    }


    @ApiOperation("获取用户通过用户id")
    @PostMapping("/getuser/userId")
    public ResponseEntity<?> getUserByUserId(@RequestParam("userId")String userId){
        User user =userFeignService.getUserByHashUserId(userId);
        log.info(JSONObject.toJSONString(user));
        return ResponseEntity.ok(user);
    }


    @ApiOperation("根据用户名获取用户名")
    @GetMapping("/getservername")
    public ResponseEntity<?> getServerName(@RequestParam("username")String username){
        String serverName=SecurityUtil.getCurrentUserServerName();
        String userName=SecurityUtil.getCurrentUsername();
        log.info("current username：{} serverName:{} time:{}",userName,serverName,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if(StringUtils.isEmpty(username)){
            return ResponseEntity.ok(Results.error("用户名不得为空"));
        }
        DataNode dataNode =userFeignService.findNodeByUsername(username);
        return ResponseEntity.ok(dataNode);
    }

    @GetMapping("getusernode")
    public DataNode getUserNode(@RequestParam("userName")String userName,
                                  @RequestParam("nodeNo")String nodeNo){
        return userFeignService.getUserNodeByNodeNo(userName,nodeNo);
    }



}
